﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Xyz.Core.Infrastructure.Extentions;
using Xyz.Core.Interfaces;
using Xyz.Core.Models;
using Xyz.Core.Models.Filters;
using Xyz.Core.Web.Filters;
using Xyz.Core.Web.Models;

namespace Xyz.Core.Web.Controllers
{
    public class Pagedatas

    {
        int Pages { get; set; }
        string Flightnums { get; set; }
    }
    [Authorize]
    
    public class HbxxController : Controller
    {
        private readonly IHbxxService _hbxxService;
        private readonly IIcaoService _icaoService;
        private readonly ITypeserverService _typeserverService;
        private readonly IAcService _acService;
        private readonly IMapper _mapper;
        static private int Zb;
        static private int Zbs;
        static private int Page;
        static private int Pages;
        static private int Ib;
        static private int Id;
        static private string Ss;
        static private string Flightnum;
        static private string Flightnums;
        static private string Starting_point;
        static private string End;
        static private string HomeIcao;
        static private string PointIcao;
        static private string Starting_pointAirport;
        static private string EndAirport;
        static private string Ac;
        static private string Type;
        static private string DayTime;

        public HbxxController(IHbxxService hbxxSvc, IIcaoService icaoSvc, ITypeserverService typeserverSvc, IAcService acSvc, IMapper mapper)
        {
            _icaoService = icaoSvc;
            _hbxxService = hbxxSvc;
            _typeserverService = typeserverSvc;
            _acService = acSvc;
            _mapper = mapper;
        }
        public IActionResult Index(string searchString, string searchStrings, string flightnum, string homeIcao, string pointIcao, string starting_point, string end, string ac, string type, string dayTime, string starting_pointAirport, string endAirport,int page)
        {
            //页面跳转
            //页面搜索
            ViewData["CurrentFilter"] = searchString;           
            
            ViewData["CurrentFilters"] = searchStrings;
            
            if (searchString != null)
            { Zb = Convert.ToInt32(searchString); }
            

            
                if (page > 0)
                { Page =page ; }


            Ss = searchStrings;
             
            //页面筛选
            ViewData["starting_points"] = starting_point;
            ViewData["ends"] = end;
            ViewData["starting_pointsAirport"] = starting_pointAirport;
            ViewData["endsAirport"] = endAirport;
            ViewData["acs"] = ac;
            ViewData["types"] = type;
            ViewData["daytimes"] = dayTime;
            ViewData["flightnums"] = flightnum;
            ViewData["homeIcao"] = homeIcao;
            ViewData["pointIcaos"] = pointIcao;
            Starting_point = starting_point;
            End = end;
            Starting_pointAirport = starting_pointAirport;
            EndAirport = endAirport;
            Ac = ac;
            Type = type;
            DayTime = dayTime;
            Flightnum = flightnum;
            HomeIcao = homeIcao;
            PointIcao = pointIcao;

            return View();
        }





        [IgnoreRightFilter]
        public IActionResult _Index(int id ,string flightnum,int page, string searchString)
        {

            if (id != 0)
            {
                Ib = id;
                Flightnums = flightnum;
            }
            if(id==0)
            { Ib = Id; }
            
            if (page > 0)
            {
                Pages = page;
            }

            ViewData["CurrentFilter"] = searchString;
            if (searchString != null)
            { Zbs = Convert.ToInt32(searchString); }
            return View();
        }

        /// <summary>
        /// 航班号
        /// </summary>
        /// <param name="id">搜索id</param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public IActionResult _Fights(string Id, string searchString,int page)
        {
             Flightnum = Id;
            //页面跳转
            //页面搜索
            //Pagedatas[] pagess = new Pagedatas[] { };
            if (page > 0)
            { Page = page; }
            ViewData["CurrentFilter"] = searchString;
            if (searchString != null)
            { Zb = Convert.ToInt32(searchString); }
            return View();
        }
        /// <summary>
        /// 机型
        /// </summary>
        /// <param name="id">搜索id</param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public IActionResult _Type(string searchString, string searchStrings, int page)
        {
            
            ViewData["CurrentFilter"] = searchString;
            ViewData["CurrentFilters"] = searchStrings;
            if (searchString != null)
            { Zb = Convert.ToInt32(searchString); }
            if (page > 0)
            { Page = page; }
            if (searchStrings != null)
            {
                Ss = searchStrings;
            }
            return View();
        }
        [IgnoreRightFilter]
        public IActionResult _Types(string id, string searchString, int page, string searchStrings)
        {
            Type = id;
            ViewData["CurrentFilter"] = searchString;
            ViewData["CurrentFilters"] = searchStrings;
            if (searchString != null)
            { Zb = Convert.ToInt32(searchString); }
            if (searchStrings != null)
            {
                Ss = searchStrings;
            }
            if (page > 0)
            { Page = page; }

            return View();
        }
        /// <summary>
        /// 航空公司
        /// </summary>
        /// <param name="id">搜索id</param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public IActionResult _Ac(string searchString, string searchStrings, int page)
        {
            ViewData["CurrentFilter"] = searchString;
            ViewData["CurrentFilters"] = searchStrings;
            if (searchString != null)
            { Zb = Convert.ToInt32(searchString); }
            if (page > 0)
            { Page = page; }
            if (searchStrings != null)
            {
                Ss = searchStrings;
            }
            return View();
        }
        [IgnoreRightFilter]
        public IActionResult _Acs(string id, string searchString, int page, string searchStrings)
        {
            Ac = id;
            ViewData["CurrentFilter"] = searchString;
            ViewData["CurrentFilters"] = searchStrings;
            if (searchString != null)
            { Zb = Convert.ToInt32(searchString); }

            if (page > 0)
            { Page = page; }
            Ss = searchStrings;
            return View();
        }

        /// <summary>
        /// 历史行情页
        /// </summary>
        /// <param name="filters">查询参数</param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithFlightnum(HbxxFilters filters)
        {
            if (Flightnums != null)
            { filters.keywords = Flightnums; }
            if (Zbs > 0)
            { filters.page = Zbs; }
            if (Zbs == 0)
            { filters.page = 1; }
            if (Pages > 0)
            { filters.page = Pages; }
            
            

            filters.rows = 15;

            var result = await _hbxxService.SearchAsyncflightnum(filters, Flightnums);
            //重置
            // Flightnums = null;
            Id = Ib;
            Ib = 0;
            Pages = 0;
            Zbs = 0;
            return Json(result);
        }



        /// <summary>
        /// 搜索 ActionResult
        /// </summary>
        /// <param name="filters">查询参数</param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithPager(HbxxFilters filters)
        {
            if (Ss != null)
            { filters.keywords = Ss; }
            if (Zb>0)
            {filters.page = Zb; }
            if (Zb == 0)
            { filters.page = 1; }
            if (Page > 0)
            { filters.page = Page; }
            Zb = 0;
            Page = 0;
            


            filters.rows =15 ;


            var result = await _hbxxService.SearchAsync(filters,Ib, Flightnum, HomeIcao,PointIcao,Starting_point, End,Ac, Type, DayTime, Starting_pointAirport, EndAirport);
            //重置
            Ib = 0;
            Flightnum = null;
            Starting_point = null;
            End = null;
            Ac = null;
            Type = null;
            DayTime = null;
            Starting_pointAirport = null;
            EndAirport = null;
            HomeIcao = null;
            PointIcao = null;
            return Json(result);
        }


        





        /// <summary>
        /// Icao坐标
        /// </summary>
        /// <param name="filters"></param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithIcao(IcaoFilters filters)
        {
            filters.page = 1;
            filters.rows = 11000;
            var result = await _icaoService.SearchAsync(filters);
            return Json(result);
        }

        /// <summary>
        /// 机型
        /// </summary>
        /// <param name="filters"></param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithType(TypeserverFilters filters)
        {
            //filters.page = 1;
            //filters.rows = 11000;
            if (Ss != null)
            { filters.keywords = Ss; }
            if (Zb > 0)
            { filters.page = Zb; }
            if (Zb == 0)
            { filters.page = 1; }
            if (Page > 0)
            { filters.page = Page; }
            Zb = 0;
            Page = 0;



            filters.rows = 15;
            var result = await _typeserverService.SearchAsync(filters);
            Ss = null;
            return Json(result);
        }
        /// <summary>
        /// 航空公司
        /// </summary>
        /// <param name="filters"></param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithAc(AcFilters filters)
        {
            if (Ss != null)
            { filters.keywords = Ss; }
            if (Zb > 0)
            { filters.page = Zb; }
            if (Zb == 0)
            { filters.page = 1; }
            if (Page > 0)
            { filters.page = Page; }
            Zb = 0;
            Page = 0;



            filters.rows = 15;
            var result = await _acService.SearchAsync(filters);
            Ss = null;
            return Json(result);
        }


    }
}